using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
using Voodoo.Meta.Utils;

namespace Voodoo.Meta
{
	[CreateAssetMenu(menuName = "Voodoo/Leaderboard/LeaderboardRankData", fileName = "LeaderboardRankData")]
	public class LeaderboardRankData : ScriptableObject
	{
		[Tooltip("The starting rank of the player.")]
		public int playerStartRank;

		[Tooltip("The animation curve describes the number of ranks gained for a defined score.")]
		public AnimationCurve rankCurve = AnimationCurve.EaseInOut(0f, 5000f, 15000f, 8f);

		[Tooltip("If true the rank step will be clamped using rankClampRange")]
		public bool allowRankClamp = true;

		[Tooltip("The rank step gained by the player is clamp in a min/max range.")]
		[MinMax(0, 1000, showEditRange = true)]
		public Vector2Int rankClampRange = new Vector2Int(8, 487);

		[FormerlySerializedAs("_colorDatas")]
		[Tooltip("A list of color scheme to apply to the leaderboard slots based on rank.")]
		public List<LeaderboardRankColorData> colorData = new List<LeaderboardRankColorData>();

		[FormerlySerializedAs("_leaderboardMedalDatas")]
		[Tooltip("A list of medal to give if a slot is in a specific range.")]
		public List<LeaderboardMedalData> medalData = new List<LeaderboardMedalData>();

		public LeaderboardBackgroundColors GetBackgroundColorsByRank(int rank, bool player = false)
		{
			LeaderboardBackgroundColors result = default(LeaderboardBackgroundColors);
			foreach (LeaderboardRankColorData colorDatum in colorData)
			{
				if (colorDatum.IsRankInRange(rank))
				{
					if (player)
					{
						result.backgroundColor = colorDatum.playerBackgroundColor;
						result.rankBackgroundColor = colorDatum.playerRankBackgroundColor;
					}
					else
					{
						result.backgroundColor = colorDatum.backgroundColor;
						result.rankBackgroundColor = colorDatum.rankBackgroundColor;
					}
					break;
				}
			}
			return result;
		}

		public Sprite GetMedalByRank(int rank)
		{
			for (int i = 0; i < medalData.Count; i++)
			{
				if (medalData[i].IsInRange(rank))
				{
					return medalData[i].MedalSprite;
				}
			}
			return null;
		}

		public int GetRankForScore(int score)
		{
			return Mathf.RoundToInt(rankCurve.Evaluate(score));
		}
	}
}
